﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WS.EKA.Model;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Data.Common;
using System.Data;

namespace WS.EKA.DataAccess
{
    public class OrderContext : DBContext<Order>
    {
        const string UspOrderSave = "Usp_OrderSave";
        const string UspOrderItemAdd = "Usp_OrderItemAdd";
        public static void Save(Order order)
        {
            List<SqlParameter> list = new List<SqlParameter>();
            
            list.Add(new SqlParameter("@MemLoginID", order.MemLoginID));
            list.Add(new SqlParameter("@OrderNumber", order.OrderNumber));
            list.Add(new SqlParameter("@Name", order.Name));
            list.Add(new SqlParameter("@Email", order.Email??string.Empty));
            list.Add(new SqlParameter("@Address", order.Address));
            list.Add(new SqlParameter("@Postalcode", order.Postalcode??string.Empty));
            list.Add(new SqlParameter("@Tel", order.Tel??string.Empty));
            list.Add(new SqlParameter("@Mobile", order.Mobile ?? string.Empty));
            list.Add(new SqlParameter("@ClientToSellerMsg", order.ClientToSellerMsg ?? string.Empty));
            list.Add(new SqlParameter("@DispatchModeGuid", order.DispatchModeGuid));
            list.Add(new SqlParameter("@PaymentGuid", order.PaymentGuid));
            list.Add(new SqlParameter("@OutOfStockOperate", order.OutOfStockOperate ?? string.Empty));
            list.Add(new SqlParameter("@PackGuid", order.PackGuid));
            list.Add(new SqlParameter("@BlessCardGuid", order.BlessCardGuid));
            list.Add(new SqlParameter("@ActivityGuid", order.ActivityGuid));
            list.Add(new SqlParameter("@InvoiceTitle", order.InvoiceTitle ?? string.Empty));
            list.Add(new SqlParameter("@InvoiceContent", order.InvoiceContent ?? string.Empty));
            list.Add(new SqlParameter("@InvoiceType", order.InvoiceType ?? string.Empty));
            list.Add(new SqlParameter("@RegionCode", order.RegionCode ?? string.Empty));
            list.Add(new SqlParameter("@JoinActiveType", order.JoinActiveType));
            list.Add(new SqlParameter("@ActvieContent", order.ActvieContent ?? string.Empty));
            list.Add(new SqlParameter("@UsedFavourTicket", order.UsedFavourTicket ?? string.Empty));
            list.Add(new SqlParameter("@LogisticsCompanyCode", order.LogisticsCompanyCode ?? string.Empty));
            list.Add(new SqlParameter("@CreateUser", order.CreateUser));
            list.Add(new SqlParameter("@ProductPrice", order.ProductPrice));
            list.Add(new SqlParameter("@DispatchPrice", order.DispatchPrice));
            list.Add(new SqlParameter("@InsurePrice", order.InsurePrice));
            list.Add(new SqlParameter("@PaymentPrice", order.PaymentPrice));
            list.Add(new SqlParameter("@PackPrice", order.PackPrice));
            list.Add(new SqlParameter("@BlessCardPrice", order.BlessCardPrice));
		    list.Add(new SqlParameter("@AlreadPayPrice", order.AlreadPayPrice));
            list.Add(new SqlParameter("@SurplusPrice", order.SurplusPrice));
            list.Add(new SqlParameter("@UseScore", order.UseScore));
            list.Add(new SqlParameter("@ScorePrice", order.ScorePrice));
            list.Add(new SqlParameter("@ShouldPayPrice", order.ShouldPayPrice));

            list.Add(new SqlParameter("@OrderId", order.Guid));
            list.Add(new SqlParameter("@ModifyUser", order.ModifyUser ?? string.Empty));

            using (var context = CreateContext())
            {
                SqlConnection conn = context.Database.Connection as SqlConnection;
                if (conn.State != System.Data.ConnectionState.Open)
                    conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                DbCommand cmd = CreateCommand(conn, tran);
                
                try
                {
                    ExecuteProc(cmd, UspOrderSave, list.ToArray());

                    foreach (var item in order.ProductList)
                    {
                        cmd = CreateCommand(conn, tran);
                        OrderItemAdd(cmd, order.Guid, item);
                        ShoppingCartContext.Delete(order.MemLoginID, item.ProductGuid);
                    }

                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }

        }
        

        public static void OrderItemAdd(DbCommand cmd, Guid orderId, OrderProduct product)
        {
            SqlParameter[] parameters = new SqlParameter[]
                {
                    new SqlParameter("@OrderInfoGuid",orderId),
                    new SqlParameter("@ProductGuid",product.ProductGuid),
                    new SqlParameter("@Attributes",product.Attributes),
                    new SqlParameter("@ExtensionAttriutes",product.ExtensionAttriutes),
                    new SqlParameter("@BuyNumber",product.BuyNumber),
                    new SqlParameter("@BuyPrice",product.BuyPrice),
                    new SqlParameter("@IsJoinActivity",product.IsJoinActivity),
                    new SqlParameter("@IsPresent",product.IsPresent)
                };

            ExecuteProc(cmd, UspOrderItemAdd, parameters);
        }

        private static DbCommand CreateCommand(DbConnection conn, SqlTransaction tran)
        {
            DbCommand cmd = conn.CreateCommand();
            if (tran != null)
            {
                cmd.Transaction = tran;
            }
            return cmd;
        }
        private static void ExecuteProc(DbCommand cmd, string procName, params DbParameter[] par)
        {
            cmd.CommandText = procName;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(par);

            cmd.ExecuteNonQuery();
        }
    }
}
